package com.sz.stock.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sz.stock.mapper.DealMapper;
import com.sz.stock.model.Deal;
import com.sz.stock.model.DealExample;
import com.sz.stock.model.PageBean;
import com.sz.stock.model.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DealServiceImp implements IDealService {

    @Autowired
    private DealMapper dealMapper;

    @Override
    public int dealAdd(Deal deal) {
        return dealMapper.insert(deal);
    }

    /**
     * 查询 成交表 带分页
     */
    @Override
    public PageBean<Deal> allDeal2(int userid, int pageIndex, int pageSize, String stockDealCode) {
        PageHelper.startPage(pageIndex, pageSize);
        DealExample example = new DealExample();
        DealExample.Criteria criteria = example.createCriteria();
        if (stockDealCode != null && !stockDealCode.equals("")) {
            criteria.andStockDealCodeEqualTo(stockDealCode);
        }
        criteria.andStockDealUseridEqualTo(userid);
        List<Deal> list = dealMapper.selectByExample(example);
        //带模糊查询并分页的数据,如果stockEntrustCode为空,则查询所有
        Page<Deal> plist = (Page<Deal>) list;
        PageBean<Deal> pb = new PageBean<>(
                plist.getTotal(), plist.getResult(),
                plist.getPageSize(), plist.getPages(), plist.getPageNum()
        );
        return pb;
    }

    /**
     * 添加 成交表信息
     *
     * @param deal
     * @return
     */
    @Override
    public int addDeal(Deal deal) {
        return dealMapper.insertSelective(deal);
    }
}
